/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.ContentChangeListener;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.TreeNodeChangeListener;
import han.bifang.gwt.client.common.TreeNodeContentListener;
import han.bifang.gwt.client.common.entity.MappingService;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceContentConstants;
import han.bifang.gwt.client.service.ServiceMessages;
import han.bifang.gwt.client.service.tree.MappingServiceVersionTree;
import han.bifang.gwt.client.service.tree.ServiceVersionTreeItem;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TreeItem;

/**
 * 
 * @Author Kent
 */
public class RemoteServiceRootContent extends Content implements
		TreeNodeContentListener, ContentChangeListener {
	private TreeItem selectedItem = null;

	private DecoratorPanel panel = null;

	private ServiceMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private TreeNodeChangeListener nodeListener = null;

	private HorizontalPanel hp;
	private String id;

	private MappingService ser;

	private DecoratorPanel treePanel;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			ArrayList ds = (ArrayList) res.getResponse();

			ser = (MappingService) ds.get(0);
			// ArrayList qos = (ArrayList) ds.get(1);
			// BusServiceRel r = (BusServiceRel) ds.get(2);
			initTree(ser);
			RemoteServiceDetailContent sdc = new RemoteServiceDetailContent(cons, service,
					context, ds, null);
			initServiceDetailContent(sdc);

			// TabPanel tab = new TabPanel();
			// tab.add(new ServiceBasicContent(cons, service, context, ser),
			// cons
			// .basic());
			// tab.add(new ServiceConfigContent(cons, service, context, qos),
			// cons
			// .config());
			// tab.add(new ServiceBusBindingContent(cons, service, context, r),
			// cons.bus());
			//
			// tab.selectTab(0);
			// tab.setWidth("100%");
			// VerticalPanel serviceContentPanel = new VerticalPanel();
			// serviceContentPanel.add(tab);
			// serviceContentPanel.add(new HTML("&nbsp;"));
			//
			// HorizontalPanel h1 = new HorizontalPanel();
			//
			// Button b = new Button(cons.addVersion());
			// b.addClickListener(new ClickListener() {
			//
			// public void onClick(Widget sender) {
			// context.put(ServiceConstants.SERVICE, ser);
			//
			// onContentChanged(ServiceContentConstants.SERVICE_ROOT,
			// ServiceContentConstants.CREATE_VERSION_BASIC, null);
			// }
			//
			// });
			//
			// h1.add(b);
			// h1.add(new HTML("&nbsp;&nbsp;"));
			//
			// b = new Button(cons.remove());
			// h1.add(b);
			// h1.add(new HTML("&nbsp;&nbsp;"));
			//
			// b = new Button(cons.ret());
			// h1.add(b);
			//
			// serviceContentPanel.add(h1);
			//

			panel.setWidget(sdc);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public void initTree(MappingService ser) {
		MappingServiceVersionTree tree = new MappingServiceVersionTree(cons, service,
				context, ser);

		tree.setTreeNodeContentListener(this);
		this.setTreeNodeChangeListener(tree);

		treePanel.add(tree);
	}

	public void initServiceDetailContent(RemoteServiceDetailContent c) {
		c.setContentChangeListener(this);
	}

	public void setTreeNodeChangeListener(TreeNodeChangeListener l) {

		this.nodeListener = l;
	}

	public void onTreeNodeSelected(TreeItem item) {
		this.selectedItem = item;
		if (item instanceof ServiceVersionTreeItem) {
			ServiceVersionTreeItem i = (ServiceVersionTreeItem) item;
			if (i.getSubType().equals(ServiceConstants.SERVICE_VERSION)) {
				RemoteVersionRootContent vrc = new RemoteVersionRootContent(cons, service,
						context, i.getKey());
				vrc.setContentChangeListener(this);
				panel.setWidget(vrc);
				return;
			} else if (i.getSubType().equals(ServiceConstants.SERVICE_ROOT)) {
				RemoteServiceDetailContent sdc = new RemoteServiceDetailContent(cons,
						service, context, null, id);
				initServiceDetailContent(sdc);
				panel.setWidget(sdc);
				return;

			}
		}

	}

	public RemoteServiceRootContent(ServiceMessages cons,
			BifangGWTServiceAsync service, GwtContext context, String id) {
		this.service = service;
		this.context = context;
		this.cons = cons;
		this.id = id;

		hp = new HorizontalPanel();
		hp.setWidth("100%");

		// hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		initWidget(hp);

	}

	public void onLoad() {
		treePanel = new DecoratorPanel();
		treePanel.setWidth("100%");

		hp.add(treePanel);

		panel = new DecoratorPanel();
		panel.setWidth("100%");
		hp.add(panel);
		hp.setCellWidth(treePanel, "25%");
		hp.setCellWidth(panel, "75%");
		RequestObject req = new RequestObject();
		req.setRequest(id);
		req
				.setAction("han.bifang.gwt.server.action.service.GetRemoteServiceDetailAction");
		service.call(req, cb);

	}

	public void onContentChanged(String source, String dest, HashMap props) {
		if (source.equals(ServiceContentConstants.REMOTE_SERVICE_ROOT)
				&& dest.equals(ServiceContentConstants.SEARCH_RESULT)) {
			this.listener.onContentChanged(ServiceContentConstants.REMOTE_SERVICE_ROOT, 
					ServiceContentConstants.SEARCH_RESULT, props);
			return;
		}

		
		if (source.equals(ServiceContentConstants.REMOTE_SERVICE_ROOT)
				&& dest.equals(ServiceContentConstants.REMOVE_REMOTE_SERVICE)) {

			listener.onContentChanged(ServiceContentConstants.REMOTE_SERVICE_ROOT, 
					ServiceContentConstants.REMOVE_REMOTE_SERVICE, props);

			return;
		}

		if (source.equals(ServiceContentConstants.REMOVE_REMOTE_SERVICE)
				&& dest.equals(ServiceContentConstants.REMOTE_SERVICE_ROOT)) {

			listener.onContentChanged(ServiceContentConstants.REMOVE_REMOTE_SERVICE, 
					ServiceContentConstants.REMOTE_SERVICE_ROOT, props);

			return;
		}

		if (source.equals(ServiceContentConstants.REMOVE_REMOTE_SERVICE)
				&& dest.equals(ServiceContentConstants.REMOTE_DOMAIN_ROOT)) {
			listener.onContentChanged(ServiceContentConstants.REMOVE_REMOTE_SERVICE, 
					ServiceContentConstants.REMOTE_DOMAIN_ROOT, props);
			return;
		}
		
		//from here, begin service root operation
		
		if (source.equals(ServiceContentConstants.REMOTE_SERVICE_ROOT)
				&& dest.equals(ServiceContentConstants.IMPORT_REMOTE_SERVICE_VERSION)) {
			ImportRemoteVersionContent src = new ImportRemoteVersionContent(cons,service, context);
			src.setContentChangeListener(this);
			panel.setWidget(src);
			return;
		}
		
		if (source.equals(ServiceContentConstants.IMPORT_REMOTE_SERVICE_VERSION)
				&& dest.equals(ServiceContentConstants.REMOTE_VERSION_ROOT)) {
			String id = (String)props.get("id");
			
			
			ServiceVersionTreeItem i = new ServiceVersionTreeItem();
			i.setHTML((String) props.get("version"));
			i.setSubType(ServiceConstants.SERVICE_VERSION);
			i.setKey((String) props.get("id"));
			nodeListener.onAddNode(i);
			
			RemoteVersionRootContent src = new RemoteVersionRootContent(cons,service, context, id);
			src.setContentChangeListener(this);
			panel.setWidget(src);
			return;
		}
		
		if (source.equals(ServiceContentConstants.REMOTE_VERSION_ROOT)
				&& dest.equals(ServiceContentConstants.REMOVE_REMOTE_VERSION)) {
			String id = (String)props.get("id");
			String version = (String)props.get("version");
			
			RemoveRemoteVersionContent src = new RemoveRemoteVersionContent(cons,service,id,version);
			src.setContentChangeListener(this);
			panel.setWidget(src);
			return;
		}
		
		if (source.equals(ServiceContentConstants.REMOVE_REMOTE_VERSION)
				&& dest.equals(ServiceContentConstants.REMOTE_SERVICE_ROOT)) {
			nodeListener.onRemoveNode(selectedItem);
			
			
			
			RemoteServiceDetailContent src = new RemoteServiceDetailContent(cons,service,context,null,id);
			src.setContentChangeListener(this);
			panel.setWidget(src);
			return;
		}


	}

}
